package com.android.settings.ios.face;

import android.animation.ObjectAnimator;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Vibrator;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.CycleInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.settings.ios.R;

import java.util.Iterator;
import java.util.LinkedList;

public class PasswordInputView extends LinearLayout implements View.OnClickListener {

    private final int MAX_CHAR = 6;

    private ImageView[] mImageResult = new ImageView[MAX_CHAR];
    private Button[] mInputButtons = new Button[10];
    private ImageButton mBtnDelete;
    private TextView mBtnCancel;
    private View mResultLayout;

    private TextView mTextTitle;
    private TextView mTextSummary;

    private final LinkedList<String> mNumberInputLink = new LinkedList<>();
    private final ContentResolver mCR;

    private OnInputChangedListener mListener;

    private boolean isInNewCreateMode = false;

    public PasswordInputView(Context context) {
        this(context, null, 0);
    }

    public PasswordInputView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PasswordInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mCR = context.getContentResolver();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mTextTitle = findViewById(android.R.id.title);
        mTextSummary = findViewById(android.R.id.summary);

        mResultLayout = findViewById(R.id.input_result_layout);

        mImageResult[0] = findViewById(R.id.iv_result_1);
        mImageResult[1] = findViewById(R.id.iv_result_2);
        mImageResult[2] = findViewById(R.id.iv_result_3);
        mImageResult[3] = findViewById(R.id.iv_result_4);
        mImageResult[4] = findViewById(R.id.iv_result_5);
        mImageResult[5] = findViewById(R.id.iv_result_6);

        mInputButtons[0] = findViewById(R.id.btn_input_0);
        mInputButtons[1] = findViewById(R.id.btn_input_1);
        mInputButtons[2] = findViewById(R.id.btn_input_2);
        mInputButtons[3] = findViewById(R.id.btn_input_3);
        mInputButtons[4] = findViewById(R.id.btn_input_4);
        mInputButtons[5] = findViewById(R.id.btn_input_5);
        mInputButtons[6] = findViewById(R.id.btn_input_6);
        mInputButtons[7] = findViewById(R.id.btn_input_7);
        mInputButtons[8] = findViewById(R.id.btn_input_8);
        mInputButtons[9] = findViewById(R.id.btn_input_9);

        for (int i = 0; i < 10; i++) {
            mInputButtons[i].setOnClickListener(this);
        }

        mBtnDelete = findViewById(R.id.btn_input_del);
        mBtnDelete.setOnClickListener(this);

        mBtnCancel = findViewById(R.id.input_cancel);
        mBtnCancel.setOnClickListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (id == R.id.btn_input_del) {
            if (mNumberInputLink.size() > 0) {
                mNumberInputLink.removeLast();
                updateImageResult();
            }
        } else if (id == R.id.input_cancel) {
            if (mListener != null) {
                mListener.onClose();
            }
        } else {
            if (v instanceof Button) {
                if (mNumberInputLink.size() < MAX_CHAR) {
                    String val = ((Button) v).getText().toString();
                    mNumberInputLink.add(val);
                    updateImageResult();
                    if (mNumberInputLink.size() == MAX_CHAR) {
                        inputFinish();
                    }
                } else {
                    inputFinish();
                }
            }
        }
    }

    public void setNewCreateMode(boolean en) {
        isInNewCreateMode = en;
        if (en) {
            mTextSummary.setText(R.string.os_face_create_password);
        } else {
            mTextSummary.setText(R.string.lockpassword_confirm_your_password_header_frp);
        }
    }

    public void setTextTitle(String title) {
        mTextTitle.setText(title);
    }

    public void setTextSummary(String summary) {
        mTextSummary.setText(summary);
    }

    public void resetAll() {
        mNumberInputLink.clear();
        updateImageResult();
    }

    public void verifyError() {
        resetAll();
    }

    private void updateImageResult() {
        int size = mNumberInputLink.size();
        for (int i = 0; i < 6; i++) {
            mImageResult[i].setSelected(i < size);
        }
    }

    private String mVerifyString = null;

    private String getInputPassword() {
        Iterator<String> it = mNumberInputLink.iterator();
        if (it.hasNext()) {
            StringBuilder builder = new StringBuilder();
            do {
                builder.append(it.next());
            } while (it.hasNext());
            return builder.toString();
        }
        return "";
    }

    private void inputFinish() {
        if (isInNewCreateMode) {
            if (mVerifyString == null) {
                mTextSummary.setText(R.string.lockpassword_confirm_your_password_header);
                mVerifyString = getInputPassword();
                resetAll();
            } else {
                if (mVerifyString.equals(getInputPassword())) {
                    if (mListener != null) {
                        mListener.onVerify(mVerifyString, mOldStr, true);
                    }
                } else {
                    resetAll();
                    mTextSummary.setText(R.string.lockpassword_confirm_passwords_dont_match);
                }
            }
        } else {
            if (mListener != null) {
                mListener.onVerify(getInputPassword(), mOldStr, false);
            }
        }
    }

    private String mOldStr;

    public void updateOldPassword(String str) {
        mOldStr = str;
    }

    public void savePassword(String str) {
//        Settings.System.putString(mCR, PasswordUtils.KEY_PSD_STR, str);
    }

    public void deletePassword(String str) {
//        Settings.System.putString(mCR, PasswordUtils.KEY_PSD_STR, "");
    }

    public void setOnInputChangedListener(OnInputChangedListener listener) {
        mListener = listener;
    }

    public interface OnInputChangedListener {
        void onClose();

        void onVerify(String str, String old, boolean matched);
    }
}
